<template>
	<view>
		<view style="overflow: hidden;position: relative;overflow: hidden;">
			<image src="https://cx.mingmeijt.com/Static/Upload/2020-05-19/5ec3b11cbef36.png"
				style="width: 100%;margin-top: 200upx;"
				mode="widthFix">
			</image>
			<button open-type="getUserInfo" @getuserinfo="wxGetUserInfo" style="background-color: #000000;color: #ffffff;position: fixed;left: 0;bottom: 350upx;width: 82%;height: 78upx;line-height: 78upx;margin: 0 9%;">点击按钮微信登录</button>
			<!-- <button @click="jump" data-src="./guanlian" style="background-color: #1db83a;color: #ffffff;position: fixed;left: 0;bottom: 220upx;width: 82%;height: 78upx;line-height: 78upx;margin: 0 9%;">去关联已有账户</button> -->
			<view style="text-align: center;color: #999;margin-top: 30upx;" @click="navTo('/pages/index/index')" >
				~~随便逛逛~~
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				res:[],
				user_id:'',
				select_user_id: ''
			};
		},
		onLoad:function(){
			this.select_user_id = uni.getStorageSync('select_user_id');
			// var that = this
			// uni.getStorage({
			// 	key: 'select_user_id',
			// 	success: (res) => {
			// 		that.select_user_id = '8888'
			// 	},
			// });
			uni.showLoading()
			this.setAjax()
		},
		// 获取用户信息
		methods:{
			wxGetUserInfo:function(res) {
				var o = this
				console.log(res)
				uni.setStorage({
					key: 'timestamp',
					data: res.timeStamp
				})
				uni.setStorage({
					key: 'token',
					data: '1be42a581e1592a85bdfe6f8174e391f'
				})
				let ress = res
				uni.login({
				 provider: "weixin",
				 success:function(loginRes) {
						var data = {}
						uni.getStorage({
							key: 'id',
							success: (res) => {
								console.log(res)
								data = {
									select_user_id: o.select_user_id,
									code: loginRes.code,
									encryptedData: ress.detail.encryptedData,
									iv: ress.detail.iv,
									rawData: ress.detail.rawData,
									signature: ress.detail.signature,
									id:  res.data //小程序转到公众号使用的账户信息,首页的时候缓存下来的
								}
								o.loading(data)
							},
							fail: () => {
								data = {
									select_user_id: o.select_user_id,
									code: loginRes.code,
									encryptedData: ress.detail.encryptedData,
									iv: ress.detail.iv,
									rawData: ress.detail.rawData,
									signature: ress.detail.signature,
								}
								o.loading(data)
							}
						});
					}
				})
				if(!res.detail.iv) {
				 uni.showModal({
					title:"温馨提示",
					content:"为了更好的购买体验，需要获取您的信息",
					success:function(resModal) {
					 if(res.confirm) {
						uni.openSetting({}) // 不支持 app h5
					 }
					}
				 });
				 return false
				}
			},
			loading (data) {
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/WxDecode/WxDecode',
					method: 'POST',
					data: data,
					header:{
						'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
						},
					success: (detail) => {
						console.log(detail)
						// 设置session过期时间
						// let d = new Date().getTime() + 8600000;
						// uni.setStorage({
						// 	key: "expire_date",
						// 	data:	d
						// })
						uni.setStorage({
							key:	"sessionid",
							data:	detail.data.session
						})
						
						uni.setStorage({
							key:	"select_user_id",
							data:	detail.data.select_user_id
							// success() {
							// 	uni.showToast({
							// 		title: detail.data.select_user_id,
							// 		icon: 'none'
							// 	})
							// }
						})
						
						
						let details = detail.data.data
						
						// 获取用户信息再存储
						if (details.status == 2) {
							uni.setStorage({
								key: 'user_name',
								data: details.user_name
							})
							uni.setStorage({
								key: 'real_name',
								data: details.real_name
							})
							uni.setStorage({
								key: 'phone_number',
								data: details.phone_number
							})
							uni.setStorage({
								key: 'province_id',
								data: details.province_id
							})
							uni.setStorage({
								key: 'city_id',
								data: details.city_id
							})
							uni.setStorage({
								key: 'district_id',
								data: details.district_id
							})
							uni.setStorage({
								key: 'level_id',
								data: details.level_id
							})
							uni.setStorage({
								key: 'real_head_img',
								data: details.real_head_img
							})
							uni.setStorage({
								key: 'user_id',
								data: details.id,
								success: function () {
									setTimeout(function() {
										uni.reLaunch({
											url: '../index/index'
										})
									}, 1700);
								}
							})
						}
					}
				});
			},
			setAjax(){
				uni.hideLoading()
			},
			navTo(url){
				uni.reLaunch({
					url
				}) 
			},
			jump(e){
				uni.navigateTo({
					url: e.currentTarget.dataset.src
				});
			},
		},
		// onUnload: function () {
		// 	uni.reLaunch({
		// 		url: '../index/index'
		// 	})
		// },
	}
</script>

<style>
	page {
		background: #FDFDFD;
	}
</style>
